package com.qiguliuxing.dts.db.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiguliuxing.dts.db.dao.DtsGoodsMapper;
import com.qiguliuxing.dts.db.domain.DtsGoods;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class DtsGoodsService {
	@Resource
	private DtsGoodsMapper goodsMapper;

	/**
	 * 获取热卖商品
	 *
	 * @param offset
	 * @param limit
	 * @return
	 */
	public List<DtsGoods> queryByHot(int offset, int limit) {
		QueryWrapper<DtsGoods> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsGoods::getIsHot, true);
		queryWrapper.lambda().eq(DtsGoods::getIsOnSale, true);
		queryWrapper.eq("deleted", false);
		queryWrapper.orderByDesc("browse");
		IPage<DtsGoods> iPage = new Page<>(offset, limit);
		return goodsMapper.selectPage(iPage, queryWrapper).getRecords();
	}

	/**
	 * 获取新品上市
	 *
	 * @param offset
	 * @param limit
	 * @return
	 */
	public List<DtsGoods> queryByNew(int offset, int limit) {
		QueryWrapper<DtsGoods> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsGoods::getIsNew, true);
		queryWrapper.lambda().eq(DtsGoods::getIsOnSale, true);
		queryWrapper.eq("deleted", false);
		queryWrapper.orderByDesc("add_time");
		IPage<DtsGoods> iPage = new Page<>(offset, limit);
		return goodsMapper.selectPage(iPage, queryWrapper).getRecords();
	}

	/**
	 * 获取分类下的商品
	 *
	 * @param catList
	 * @param offset
	 * @param limit
	 * @return
	 */
	public List<DtsGoods> queryByCategory(List<Integer> catList, int offset, int limit) {
		QueryWrapper<DtsGoods> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().in(DtsGoods::getCategoryId, catList);
		queryWrapper.lambda().eq(DtsGoods::getIsOnSale, true);
		queryWrapper.eq("deleted", false);
		queryWrapper.lambda().orderByDesc(DtsGoods::getSortOrder);
		IPage<DtsGoods> iPage = new Page<>(offset, limit);
		return goodsMapper.selectPage(iPage, queryWrapper).getRecords();
	}

	public List<DtsGoods> querySelective(Integer catId, Integer brandId, String keywords, Boolean isHot, Boolean isNew,
			Integer offset, Integer limit, String sort) {
		QueryWrapper<DtsGoods> queryWrapper = new QueryWrapper<>();
//		DtsGoodsExample example = new DtsGoodsExample();
//		DtsGoodsExample.Criteria criteria1 = example.or();
//		DtsGoodsExample.Criteria criteria2 = example.or();

		if (catId != null && catId != 0) {
			queryWrapper.lambda().eq(DtsGoods::getCategoryId, catId);
//			criteria1.andCategoryIdEqualTo(catId);
//			criteria2.andCategoryIdEqualTo(catId);
		}
		if (brandId != null) {
			queryWrapper.lambda().eq(DtsGoods::getBrandId, brandId);
//			criteria1.andBrandIdEqualTo(brandId);
//			criteria2.andBrandIdEqualTo(brandId);
		}
		if (isNew != null) {
			queryWrapper.lambda().eq(DtsGoods::getIsNew, isNew);
//			criteria1.andIsNewEqualTo(isNew);
//			criteria2.andIsNewEqualTo(isNew);
		}
		if (isHot!= null) {
			queryWrapper.lambda().eq(DtsGoods::getIsHot, isHot);
			// queryWrapper.eq("isHot", isHot);
//			criteria1.andIsHotEqualTo(isHot);
//			criteria2.andIsHotEqualTo(isHot);
		}
		if (StrUtil.isNotEmpty(keywords)) {
			queryWrapper.lambda().and(i -> i.like(DtsGoods::getKeywords, keywords).or().like(DtsGoods::getName, keywords));
//			criteria1.andKeywordsLike("%" + keywords + "%");
//			criteria2.andNameLike("%" + keywords + "%");
		}
		queryWrapper.lambda().eq(DtsGoods::getIsOnSale, true);
		queryWrapper.eq("deleted", false);

		if (StrUtil.isNotEmpty(sort)) {
			queryWrapper.orderByDesc(sort);
		}
		IPage<DtsGoods> iPage = new Page<>(offset, limit);
		return goodsMapper.selectPage(iPage, queryWrapper).getRecords();
//		criteria1.andIsOnSaleEqualTo(true);
//		criteria2.andIsOnSaleEqualTo(true);
//		criteria1.andDeletedEqualTo(false);
//		criteria2.andDeletedEqualTo(false);
	}

	public List<DtsGoods> querySelective(String goodsSn, String name, Integer page, Integer size, String sort,
			String order,List<Integer> brandIds) {
		QueryWrapper<DtsGoods> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsGoods::getGoodsSn, goodsSn);
		if (StrUtil.isNotEmpty(name)) {
			queryWrapper.like("name", name);
		}
		queryWrapper.eq("deleted", false);
		if (brandIds != null && brandIds.size() > 0) {
			queryWrapper.lambda().in(DtsGoods::getBrandId, brandIds);
		}
		if (StrUtil.isNotEmpty(sort) && StrUtil.isNotEmpty(order)) {
			queryWrapper.orderByDesc(sort);
		}
		IPage<DtsGoods> iPage = new Page<>(page, size);
		return goodsMapper.selectPage(iPage, queryWrapper).getRecords();
	}

	/**
	 * 获取某个商品信息,包含完整信息
	 *
	 * @param id
	 * @return
	 */
	public DtsGoods findById(Integer id) {
		QueryWrapper<DtsGoods> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id);
		queryWrapper.eq("deleted", false);
		return goodsMapper.selectOne(queryWrapper);
	}
	
	/**
	 * 根据序列码找到商品
	 * @param goodsSn
	 * @return
	 */
	public DtsGoods findByGoodsSn(String goodsSn) {
		QueryWrapper<DtsGoods> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsGoods::getGoodsSn, goodsSn);
		queryWrapper.eq("deleted", false);
		return goodsMapper.selectOne(queryWrapper);
	}

	/**
	 * 获取某个商品信息，仅展示相关内容
	 *
	 * @param id
	 * @return
	 */
	public DtsGoods findByIdVO(Integer id) {
		QueryWrapper<DtsGoods> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id);
		queryWrapper.lambda().eq(DtsGoods::getIsOnSale, true);
		queryWrapper.eq("deleted", false);
		return goodsMapper.selectOne(queryWrapper);
	}
	
	public DtsGoods findBySnVO(String sn) {
		QueryWrapper<DtsGoods> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsGoods::getGoodsSn, sn);
		queryWrapper.lambda().eq(DtsGoods::getIsOnSale, true);
		queryWrapper.eq("deleted", false);
		return goodsMapper.selectOne(queryWrapper);
	}

	/**
	 * 获取所有在售物品总数
	 *
	 * @return
	 */
	public Integer queryOnSale() {
		QueryWrapper<DtsGoods> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsGoods::getIsOnSale, true);
		queryWrapper.eq("deleted", false);
		return goodsMapper.selectCount(queryWrapper).intValue();
	}

	public int updateById(DtsGoods goods) {
		goods.setUpdateTime(LocalDateTime.now());
		return goodsMapper.updateById(goods);
	}

	public void deleteById(Integer id) {
		goodsMapper.deleteById(id);
	}

	public void add(DtsGoods goods) {
		goods.setAddTime(LocalDateTime.now());
		goods.setUpdateTime(LocalDateTime.now());
		goodsMapper.insert(goods);
	}

	/**
	 * 获取所有物品总数，包括在售的和下架的，但是不包括已删除的商品
	 *
	 * @return
	 */
	public int count() {
		QueryWrapper<DtsGoods> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("deleted", false);
		return goodsMapper.selectCount(queryWrapper).intValue();
	}

	public List<Integer> getCatIds(Integer brandId, String keywords, Boolean isHot, Boolean isNew) {
		QueryWrapper<DtsGoods> queryWrapper = new QueryWrapper<>();
		if (brandId != null) {
			queryWrapper.lambda().eq(DtsGoods::getBrandId, brandId);
		}
		if (isNew != null) {
			queryWrapper.lambda().eq(DtsGoods::getIsNew, isNew);
		}
		if (isHot!= null) {
			queryWrapper.lambda().eq(DtsGoods::getIsHot, isHot);
		}
		if (StrUtil.isNotEmpty(keywords)) {
			queryWrapper.lambda().and(i -> i.like(DtsGoods::getKeywords, keywords).or().like(DtsGoods::getName, keywords));
		}
		queryWrapper.lambda().eq(DtsGoods::getIsOnSale, true);
		queryWrapper.eq("deleted", false);

		List<DtsGoods> goodsList = goodsMapper.selectList(queryWrapper);
		List<Integer> cats = new ArrayList<Integer>();
		for (DtsGoods goods : goodsList) {
			cats.add(goods.getCategoryId());
		}
		return cats;
	}

	public boolean checkExistByName(String name) {
		QueryWrapper<DtsGoods> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("name", name);
		queryWrapper.lambda().eq(DtsGoods::getIsOnSale, true);
		queryWrapper.eq("deleted", false);
		return goodsMapper.selectCount(queryWrapper) != 0;
	}

	/**
	 * 根据店铺，获取店铺对应类别的商品
	 *
	 * @return
	 */
	public List<DtsGoods> queryByBrandId(int bid, int cid, int offset, int limit) {
		QueryWrapper<DtsGoods> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsGoods::getBrandId, bid);
		queryWrapper.lambda().eq(DtsGoods::getCategoryId, cid);
		queryWrapper.lambda().eq(DtsGoods::getIsOnSale, true);
		queryWrapper.eq("deleted", false);
		queryWrapper.orderByDesc("browse");
		IPage<DtsGoods> iPage = new Page<>(offset, limit);
		return goodsMapper.selectPage(iPage, queryWrapper).getRecords();
	}

	/**
	 * 同类商品，且不同店铺
	 *
	 * @return
	 */
	public List<DtsGoods> queryByCategoryAndNotSameBrandId(int bid, int cid, int offset, int limit) {
		QueryWrapper<DtsGoods> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsGoods::getBrandId, bid);
		queryWrapper.lambda().ne(DtsGoods::getCategoryId, cid);
		queryWrapper.lambda().eq(DtsGoods::getIsOnSale, true);
		queryWrapper.eq("deleted", false);
		queryWrapper.orderByDesc("browse");
		IPage<DtsGoods> iPage = new Page<>(offset, limit);
		return goodsMapper.selectPage(iPage, queryWrapper).getRecords();
	}
}
